package cn.ven.controller;

import cn.ven.entity.Hot;
import cn.ven.service.HotService;
import cn.ven.service.LargeFileService;
import cn.ven.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/adm/hot")
public class HotController {

    @Autowired
    HotService hotService;

    @Autowired
    LargeFileService largeFileService;

    //添加热图
    @RequestMapping("/addHot")
    @ResponseBody
    public Map<String,Object> addHot(Hot hot,
                                     @RequestParam(value = "pic",required = false)MultipartFile file)throws Exception{
        String id = largeFileService.add(file);
        hot.setPicture(id);//把图片id与书本图片进行绑定
        hotService.addHot(hot);
        return RespJson.pack(200,"添加成功",hot);
    }

    //查找所有的热图
    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String,Object> findAll(){
        List<Hot> hots = hotService.findAll();
        return RespJson.pack(200,"",hots);
    }

    //删除热图
    @RequestMapping("/delHot/{id}")
    @ResponseBody
    public Map<String,Object> delHot(@PathVariable("id")Integer id){
        hotService.delHot(id);
        return RespJson.pack(200,"删除成功","");
    }

}
